package com.example.administrator.testdagger.countdown;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.testdagger.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Description: 
 * ModelName:
 *
 * @author WangJQ
 *Create at:  2018/6/2 9:22
 * Company: 沈阳艾尔时代科技发展有限公司
 * Copyright: (c)2018 AIR Times Inc. All rights reserved.
 * @version 1.0
 */

public class ProgressAdapter2 extends RecyclerView.Adapter<ProgressAdapter2.ViewHolder> {

    private ArrayList<Date> mArrayList;
    private Context mContext;

    private SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");

    public ProgressAdapter2(Context context) {
        mContext = context;
        intList();
    }

    private void intList() {
        mArrayList = new ArrayList<>();
        mArrayList.add(new Date(System.currentTimeMillis() + 100000));
        mArrayList.add(new Date(System.currentTimeMillis() + 200000));
        mArrayList.add(new Date(System.currentTimeMillis() + 120300));
        mArrayList.add(new Date(System.currentTimeMillis() + 300220));
        mArrayList.add(new Date(System.currentTimeMillis() + 501000));
        mArrayList.add(new Date(System.currentTimeMillis() + 800000));
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pb_2, parent, false);
        return new ViewHolder(view);
    }

    public void refresh(){
        this.notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Date date = new Date();
        long now = date.getTime();
        long end = mArrayList.get(position).getTime();
        long time = end - now;

        if(time < 0){
            holder.mTv.setText("now date is: " + now + "" +
                    "\nend date is: " + end+"\n\n倒计时结束：");
        }else{
            holder.mTv.setText("now date is: " + now + "" +
                    "\nend date is: " + end+"\n\n倒计时为："+df.format(new Date(time)));
        }

    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTv;

        public ViewHolder(View itemView) {
            super(itemView);
            mTv = itemView.findViewById(R.id.tv);
        }
    }
}
